package com.ruoyi.common.utils;

import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.TypeReference;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.core.domain.model.Company;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.spring.SpringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.lang.reflect.Field;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Objects;

/**
 * 公司工具类
 *
 * @author ruoyi
 */
public class CompanyUtils {

    private static Logger log = LoggerFactory.getLogger(CompanyUtils.class);
    /**
     * 设置公司缓存
     *
     * @param company 公司数据
     */
    public static void setCompanyCache(Company company) {
        SpringUtils.getBean(RedisCache.class).setCacheObject(getCacheKey(), company);
    }

    /**
     * 获取公司信息
     *
     * @return  公司信息
     */
    public static Company getCompanyCache() {
        return SpringUtils.getBean(RedisCache.class).getCacheObject(getCacheKey());
    }

    /**
     * 获取公司某个字段的信息
     *
     * @param fieldName 字段名称
     * @return 字段值
     */
    @Deprecated
    public static String getValueByField(String fieldName) {
        try {
            Company company = getCompanyCache();
            if (Objects.isNull(company)) {
                log.warn("获取公司数据失败,降级从配置文件中获取");
                company = resetCompany();
            }
            if (Objects.isNull(company)) {
                throw new ServiceException("获取公司数据失败");
            }
            Field field = Company.class.getDeclaredField(fieldName);
            field.setAccessible(true);
            return (String) field.get(company);
        } catch (NoSuchFieldException e) {
            throw new ServiceException(StringUtils.format("公司字段{}不存在,获取失败", fieldName));
        } catch (IllegalAccessException e) {
            throw new ServiceException(StringUtils.format("公司字段{}访问失败", fieldName));
        }
    }

    /**
     * 设置公司字段值
     *
     * @param fieldName 字段名
     * @param value     值
     */
    @Deprecated
    public static void setFieldValue(String fieldName, String value) {
        try {
            Company company = getCompanyCache();
            if (Objects.isNull(company)) {
                log.warn("获取公司数据失败,降级从配置文件中获取");
                company = resetCompany();
            }
            if (Objects.isNull(company)) {
                throw new ServiceException("获取公司数据失败,设置公司数据失败");
            }
            Field field = Company.class.getDeclaredField(fieldName);
            field.setAccessible(true);
            field.set(company, value);
            setCompanyCache(company);
        } catch (NoSuchFieldException e) {
            throw new ServiceException(StringUtils.format("公司字段{}不存在,设置值失败", fieldName));
        } catch (IllegalAccessException e) {
            throw new ServiceException(StringUtils.format("公司字段{}设置值失败", fieldName));
        }
    }

    /**
     * 重置公司信息
     */
    @Deprecated
    public static Company resetCompany() {
        try {
            String content = new String(Files.readAllBytes(Paths.get(RuoYiConfig.getCompanyDataPath())));
            log.warn(StringUtils.format("重置公司信息,获取到数据:{}", content));
            if (!StringUtils.isEmpty(content)) {
                Company company = JSONObject.parseObject(content, new TypeReference<Company>() {});
                setCompanyCache(company);
                return company;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 设置cache key
     *
     * @return 缓存键key
     */
    public static String getCacheKey() {
        return CacheConstants.COMPANY_KEY;
    }
}
